package com.insurance.utils;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qcjstudy
 * @create 2022-11-30 12:26
 */
public class BeanCopyUtils {
    private BeanCopyUtils() {
    }

    //单个对象拷贝
    public static <V> V copyBean(Object source,Class<V> clazz) {
        //创建目标对象，，返回结果
        V result = null;
        try {
            result = clazz.newInstance();
            //实现属性拷贝
            BeanUtils.copyProperties(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //list集合拷贝
    public static <O,V> List<V> copyBeanList(List<O>list,Class<V> clazz){

        //函数式编程
        return list.stream()
                .map(o->copyBean(o,clazz))
                .collect(Collectors.toList());

    }
    public static void main(String[] args) {
        //Article article = new Article();
        //article.setId(1L);
        //article.setTitle("ss");
        //HotArticleVo hotArticleVo = copyBean(article, HotArticleVo.class);
        //System.out.println(hotArticleVo);
    }

}
